﻿// -------------------------------------------------------------------------------------------------------------------------------
// <copyright file="TestAssignIndexedCMFlattenDenseMatrix.cs" company="Exul">
//     Copyright (c) Exul. All rights reserved.
// </copyright>
// <author>
//     Exul
// </author>
// -------------------------------------------------------------------------------------------------------------------------------

namespace ExulLibrary.Mathematics.LinearAlgebra.Sequential.Tests.CMFlattenDenseMatrixTest
{
    #region Usings

    using System;
    using System.Globalization;

    using ExulLibrary.Exceptions;

    using NUnit.Framework;

    #endregion

    /// <summary>
    ///     Test suite for
    ///     <see cref="CMFlattenDenseMatrixExtensions.Assign{T}(CMFlattenDenseMatrix{T},int[],int[],CMFlattenDenseMatrix{T})"/>.
    /// </summary>
    [TestFixture]
    [Category("ExulLibrary.Mathematics.LinearAlgebra.Sequential.CMFlattenDenseMatrixExtensions")]
    public sealed class TestAssignIndexedCMFlattenDenseMatrix
    {
        /// <summary>
        ///     Tests the method when indexes of target elements are non-negative and less than
        ///     size of dimension of target matrix.
        /// </summary>
        [Test]
        public void WhenTargetIndexesAreValid()
        {
            // Arrange
            var targetData = new double[]
                             {
                                 1, 2, 3,
                                 4, 5, 6,
                                 7, 8, 9
                             };
            var target = new CMFlattenDenseMatrix<double>(3, 3, targetData);

            var targetRowsIndexes = new[] { 1, 2 };
            var targetColumnsIndexes = new[] { 0, 2 };
            var sourceData = new double[]
                             {
                                 10, 20,
                                 30, 40
                             };
            var source = new CMFlattenDenseMatrix<double>(2, 2, sourceData);
            var expectedResult = new double[]
                                 {
                                     1, 10, 20,
                                     4, 5, 6,
                                     7, 30, 40
                                 };

            // Act
            target.Assign(targetRowsIndexes, targetColumnsIndexes, source);

            // Assert
            Assert.That(target.Values, Is.EquivalentTo(expectedResult));
        }

        /// <summary>
        ///     Tests the method when target rows indexes collection is empty.
        /// </summary>
        [Test]
        public void WhenRowsIndexesCollectionIsEmpty()
        {
            // Arrange
            var targetData = new double[]
                             {
                                 1, 2, 3,
                                 4, 5, 6,
                                 7, 8, 9
                             };
            var target = new CMFlattenDenseMatrix<double>(3, 3, targetData);

            var targetRowsIndexes = new int[0];
            var targetColumnsIndexes = new[] { 0, 2 };
            var sourceData = new double[]
                             {
                                 10, 20,
                                 30, 40
                             };
            var source = new CMFlattenDenseMatrix<double>(2, 2, sourceData);
            var expectedResult = new double[]
                                 {
                                     1, 2, 3,
                                     4, 5, 6,
                                     7, 8, 9
                                 };

            // Act
            target.Assign(targetRowsIndexes, targetColumnsIndexes, source);

            // Assert
            Assert.That(target.Values, Is.EquivalentTo(expectedResult));
        }

        /// <summary>
        ///     Tests the method when target columns indexes collection is empty.
        /// </summary>
        [Test]
        public void WhenColumnsIndexesCollectionIsEmpty()
        {
            // Arrange
            var targetData = new double[]
                             {
                                 1, 2, 3,
                                 4, 5, 6,
                                 7, 8, 9
                             };
            var target = new CMFlattenDenseMatrix<double>(3, 3, targetData);

            var targetRowsIndexes = new[] { 1, 2 };
            var targetColumnsIndexes = new int[0];
            var sourceData = new double[]
                             {
                                 10, 20,
                                 30, 40
                             };
            var source = new CMFlattenDenseMatrix<double>(2, 2, sourceData);
            var expectedResult = new double[]
                                 {
                                     1, 2, 3,
                                     4, 5, 6,
                                     7, 8, 9
                                 };

            // Act
            target.Assign(targetRowsIndexes, targetColumnsIndexes, source);

            // Assert
            Assert.That(target.Values, Is.EquivalentTo(expectedResult));
        }

#if SAFE

        /// <summary>
        ///     Tests the method when target is <see langword="null"/>.
        /// </summary>
        [Test]
        public void WhenAugendIsNull()
        {
            // Arrange
            var targetRowsIndexes = new[] { 1, 2 };
            var targetColumnsIndexes = new[] { 0, 2 };
            var sourceData = new double[]
                             {
                                 10, 20,
                                 30, 40
                             };
            var source = new CMFlattenDenseMatrix<double>(2, 2, sourceData);

            // Act and Assert
            Assert.That(
                () => CMFlattenDenseMatrixExtensions.Assign(null, targetRowsIndexes, targetColumnsIndexes, source),
                Throws.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("target"));
        }

        /// <summary>
        ///     Tests the method when source is <see langword="null"/>.
        /// </summary>
        [Test]
        public void WhenSourceIsNull()
        {
            // Arrange
            var targetData = new double[]
                             {
                                 1, 2, 3,
                                 4, 5, 6,
                                 7, 8, 9
                             };
            var target = new CMFlattenDenseMatrix<double>(3, 3, targetData);

            var targetRowsIndexes = new[] { 1, 2 };
            var targetColumnsIndexes = new[] { 0, 2 };

            // Act and Assert
            Assert.That(
                () => target.Assign(targetRowsIndexes, targetColumnsIndexes, null),
                Throws.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("source"));
        }

        /// <summary>
        ///     Tests the method when target rows indexes collection is <see langword="null"/>.
        /// </summary>
        [Test]
        public void WhenTargetRowsIndexesCollectionIsNull()
        {
            // Arrange
            var targetData = new double[]
                             {
                                 1, 2, 3,
                                 4, 5, 6,
                                 7, 8, 9
                             };
            var target = new CMFlattenDenseMatrix<double>(3, 3, targetData);

            var targetColumnsIndexes = new[] { 0, 2 };
            var sourceData = new double[]
                             {
                                 10, 20,
                                 30, 40
                             };
            var source = new CMFlattenDenseMatrix<double>(2, 2, sourceData);

            // Act and Assert
            Assert.That(
                () => target.Assign(null, targetColumnsIndexes, source),
                Throws.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("targetRowsIndexes"));
        }

        /// <summary>
        ///     Tests the method when target columns indexes collection is <see langword="null"/>.
        /// </summary>
        [Test]
        public void WhenTargetColumnsIndexesCollectionIsNull()
        {
            // Arrange
            var targetData = new double[]
                             {
                                 1, 2, 3,
                                 4, 5, 6,
                                 7, 8, 9
                             };
            var target = new CMFlattenDenseMatrix<double>(3, 3, targetData);

            var targetRowsIndexes = new[] { 1, 2 };
            var sourceData = new double[]
                             {
                                 10, 20,
                                 30, 40
                             };
            var source = new CMFlattenDenseMatrix<double>(2, 2, sourceData);

            // Act and Assert
            Assert.That(
                () => target.Assign(targetRowsIndexes, null, source),
                Throws.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("targetColumnsIndexes"));
        }

        /// <summary>
        ///     Tests the method when a target row index is negative or greater than or equal to rows count of target matrix.
        /// </summary>
        /// <param name="rowIndex">
        ///     The row index.
        /// </param>
        /// <param name="location">
        ///     The location of row index.
        /// </param>
        [Test]
        public void WhenATargetRowIndexIsOutOfRange([Values(-1, 3)] int rowIndex, [Values(0, 1)] int location)
        {
            // Arrange
            var targetData = new double[]
                             {
                                 1, 2, 3,
                                 4, 5, 6,
                                 7, 8, 9
                             };
            var target = new CMFlattenDenseMatrix<double>(3, 3, targetData);

            var targetRowsIndexes = new int[2];
            targetRowsIndexes[location] = rowIndex;

            var targetColumnsIndexes = new[] { 0, 2 };
            var sourceData = new double[]
                             {
                                 10, 20,
                                 30, 40
                             };
            var source = new CMFlattenDenseMatrix<double>(2, 2, sourceData);

            // Act and Assert
            var argumentName = string.Format(CultureInfo.InvariantCulture, "targetRowsIndexes[{0}]", location);
            Assert.That(
                () => target.Assign(targetRowsIndexes, targetColumnsIndexes, source),
                Throws.TypeOf<ElementIndexIsOutOfRangeException>().With.Property("ParamName").EqualTo(argumentName));
        }

        /// <summary>
        ///     Tests the method when a target column index is negative or greater than or equal to
        ///     columns count of target matrix.
        /// </summary>
        /// <param name="columnIndex">
        ///     The column index.
        /// </param>
        /// <param name="location">
        ///     The location of column index.
        /// </param>
        [Test]
        public void WhenATargetColumnIndexIsOutOfRange([Values(-1, 3)] int columnIndex, [Values(0, 1)] int location)
        {
            // Arrange
            var targetData = new double[]
                             {
                                 1, 2, 3,
                                 4, 5, 6,
                                 7, 8, 9
                             };
            var target = new CMFlattenDenseMatrix<double>(3, 3, targetData);

            var targetRowsIndexes = new[] { 1, 2 };
            var targetColumnsIndexes = new int[2];
            targetColumnsIndexes[location] = columnIndex;
            var sourceData = new double[]
                             {
                                 10, 20,
                                 30, 40
                             };
            var source = new CMFlattenDenseMatrix<double>(2, 2, sourceData);

            // Act and Assert
            var argumentName = string.Format(CultureInfo.InvariantCulture, "targetColumnsIndexes[{0}]", location);
            Assert.That(
                () => target.Assign(targetRowsIndexes, targetColumnsIndexes, source),
                Throws.TypeOf<ElementIndexIsOutOfRangeException>().With.Property("ParamName").EqualTo(argumentName));
        }

#endif
    }
}